package databaseConnect;

import java.util.Vector;

public class HostConfigurationUpdateOrInsertProcessor {

	public static boolean setHostParameterRow(Vector<String> hpr) {
		try {
			int locId = Integer.parseInt(hpr.get(0));
			Vector<Vector<String>> temp = AccessConnect
					.executeSelect("SELECT COUNT(*) FROM HostDescription WHERE ID="
							+ locId);
			if (temp.get(0).get(0).equals("0")) {
				return insertNewReadyHostParameterRows(hpr);
			} else {
				return updateReadyHostParameterRow(hpr, locId);
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public static boolean setHostParameterRows(Vector<Vector<String>> hpr) {
		try {
			int locId = Integer.parseInt(hpr.firstElement().get(1));
			Vector<Vector<String>> temp = AccessConnect
					.executeSelect("SELECT COUNT(*) FROM HostDescription WHERE ID="
							+ locId);
			if (temp.get(0).get(0).equals("0")) {
				return insertNewHostParameterRows(hpr);
			} else {
				return updateHostParameterRows(hpr, locId);
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public static boolean setHostServices(Vector<String> hsr, int id) {
		try {
			AccessConnect.executeQuery(("DELETE FROM HostServices WHERE HOSTID=" + id));
			Vector<Vector<String>> temp = new Vector<Vector<String>>();
			for (int i= 0 ; i < hsr.size(); i++){
				Vector<String> loc = new Vector<String>();
				loc.add(""+id);
				loc.add(hsr.get(i));
				temp.add(loc);
			}
			return AccessConnect.executeInsert("HostServices", temp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}	
}

	public static boolean insertNewHostParameterRows(Vector<Vector<String>> hpr) {
		try {
			return AccessConnect.executeInsert("HostDescription", unpivot(hpr));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public static boolean insertNewReadyHostParameterRows(Vector<String> hpr) {
		try {
			Vector<Vector<String>> tmptmp = new Vector<Vector<String>>();
			tmptmp.add(hpr);			
			return AccessConnect.executeInsert("HostDescription", tmptmp);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	
	public static boolean updateHostParameterRows(Vector<Vector<String>> hpr,
			int id) {
		try {
			String statement = "UPDATE hostDescription SET ";
			for (int i = 1; i < hpr.size(); i++) {
				statement = statement + hpr.get(i).get(0);
				statement = statement + "=";
				statement = statement + hpr.get(i).get(1);
				if (i < hpr.size() - 1) {
					statement = statement + ", ";
				}
			}
			statement = statement + " WHERE ID=" + id;
//			System.out.println(statement);
			AccessConnect.executeQuery(statement);
			return true;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public static boolean updateReadyHostParameterRow(Vector<String> hpr,
			int id) {
		try {
			String statement = "UPDATE hostDescription SET ";
			
			statement = statement + "xPosition";
			statement = statement + "=";
			statement = statement + hpr.get(1);
		
			statement = statement + ",yPosition";
			statement = statement + "=";
			statement = statement + hpr.get(2);
		
			statement = statement + ",DiscoveryProtocol";
			statement = statement + "=";
			statement = statement + hpr.get(3);
		
			statement = statement + " WHERE ID=" + id;
			//System.out.println(statement);
			AccessConnect.executeQuery(statement);
			return true;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	private static Vector<Vector<String>> unpivot(Vector<Vector<String>> input) {
		Vector<Vector<String>> tmp = new Vector<Vector<String>>();
		Vector<String> content = new Vector<String>();
		for (int i = 0; i < input.size(); i++) {
			content.add(input.get(i).lastElement());
		}
		tmp.add(content);
		return tmp;
	}

}
